package com.hm.recycle.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.hm.common.model.PageInfo;
import com.hm.common.util.DbDocumentDef;
import com.hm.recycle.mongo.domain.DbDocumentUpdateRecycle;
import com.hm.recycle.mongo.domain.QDbDocumentUpdateRecycle;
import com.hm.recycle.mongo.vo.UpdateRecycleVo;
import com.hm.recycle.share.repository.DbDocumentUpdateRecycleRepository;
import com.hm.recycle.share.service.DbDocumentUpdateRecycleService;

/**
 * @author shishun.wang
 * @date 2016年5月23日 下午11:16:44
 * @version 1.0
 * @describe
 */
@Service
@SuppressWarnings({ "rawtypes", "unchecked" })
public class DbDocumentUpdateRecycleServiceImpl implements DbDocumentUpdateRecycleService {

	@Autowired
	private DbDocumentUpdateRecycleRepository dbDocumentUpdateRecycleRepository;

	@Override
	public String addDbDocumentUpdateRecycle(DbDocumentUpdateRecycle dbDocumentUpdateRecycle) {
		return dbDocumentUpdateRecycleRepository.save(dbDocumentUpdateRecycle).getId();
	}

	@Override
	public List<DbDocumentUpdateRecycle> queryDbDocumentUpdateRecycle(DbDocumentDef documentDef, String targetId) {
		QDbDocumentUpdateRecycle dbDocumentUpdateRecycle = QDbDocumentUpdateRecycle.dbDocumentUpdateRecycle;
		return (List<DbDocumentUpdateRecycle>) dbDocumentUpdateRecycleRepository
				.findAll(dbDocumentUpdateRecycle.targetId.eq(targetId), dbDocumentUpdateRecycle.version.desc());
		// return (List<DbDocumentUpdateRecycle>)
		// dbDocumentUpdateRecycleRepository.findAll(
		// dbDocumentUpdateRecycle.targetId.eq(targetId)
		// .and(dbDocumentUpdateRecycle.documentDef.eq(documentDef)),
		// dbDocumentUpdateRecycle.version.desc());
	}

	@Override
	public PageInfo<UpdateRecycleVo> queryUpdateRecycle(DbDocumentDef documentDef, String operationUser,
			Integer version, Long startTime, Long endTime, Integer pageNumber, Integer pageSize) {
		PageInfo<UpdateRecycleVo> pageInfo = new PageInfo<UpdateRecycleVo>(pageNumber, pageSize);

		QDbDocumentUpdateRecycle dbDocumentUpdateRecycle = QDbDocumentUpdateRecycle.dbDocumentUpdateRecycle;
		Pageable pageable = new QPageRequest(pageInfo.getPageNumber(), pageInfo.getPageSize(),
				dbDocumentUpdateRecycle.createTime.desc());

		List<UpdateRecycleVo> updateRecycleVos = new ArrayList<UpdateRecycleVo>();

		Page page = dbDocumentUpdateRecycleRepository.findAll(pageable);
		List<DbDocumentUpdateRecycle> content = page.getContent();
		for (DbDocumentUpdateRecycle updateRecycle : content) {
			UpdateRecycleVo updateRecycleVo = new UpdateRecycleVo();
			{
				updateRecycleVo.setId(updateRecycle.getId());
				updateRecycleVo.setDocumentDef(updateRecycle.getDocumentDef());
				updateRecycleVo.setOperationUser(updateRecycle.getOperationUser());
				updateRecycleVo.setVersion(updateRecycle.getVersion());
				updateRecycleVo.setTargetContent(updateRecycle.getTargetContent());
			}

			updateRecycleVos.add(updateRecycleVo);
		}
		return pageInfo.execute(page, updateRecycleVos);
	}

}
